package com.ruoyi.hire.service.impl;

import java.util.List;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.constant.CompanyConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hire.domain.vo.CompanyRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hire.mapper.HireCompanyMapper;
import com.ruoyi.hire.domain.HireCompany;
import com.ruoyi.hire.service.IHireCompanyService;

import javax.annotation.Resource;

/**
 * 用人单位管理Service业务层处理
 *
 * @author linyang
 * @date 2025-07-21
 */
@Service
public class HireCompanyServiceImpl implements IHireCompanyService {

    @Resource
    private HireCompanyMapper hireCompanyMapper;

    /**
     * 查询用人单位管理
     *
     * @param id 用人单位管理主键
     * @return 用人单位管理
     */
    @Override
    public HireCompany selectHireCompanyById(Long id) {
        return hireCompanyMapper.selectHireCompanyById(id);
    }

    /**
     * 查询用人单位管理列表
     *
     * @param hireCompany 用人单位管理
     * @return 用人单位管理
     */
    @Override
    public List<HireCompany> selectHireCompanyList(HireCompany hireCompany) {
        return hireCompanyMapper.selectHireCompanyList(hireCompany);
    }

    /**
     * 新增用人单位管理
     *
     * @param hireCompany 用人单位管理
     * @return 结果
     */
    @Override
    public int insertHireCompany(HireCompany hireCompany) {
        return hireCompanyMapper.insertHireCompany(hireCompany);
    }

    /**
     * 修改用人单位管理
     *
     * @param hireCompany 用人单位管理
     * @return 结果
     */
    @Override
    public int updateHireCompany(HireCompany hireCompany) {
        hireCompany.setUpdateTime(DateUtils.getNowDate());
        return hireCompanyMapper.updateHireCompany(hireCompany);
    }

    /**
     * 批量删除用人单位管理
     *
     * @param ids 需要删除的用人单位管理主键
     * @return 结果
     */
    @Override
    public int deleteHireCompanyByIds(Long[] ids) {
        return hireCompanyMapper.deleteHireCompanyByIds(ids);
    }

    /**
     * 删除用人单位管理信息
     *
     * @param id 用人单位管理主键
     * @return 结果
     */
    @Override
    public int deleteHireCompanyById(Long id) {
        return hireCompanyMapper.deleteHireCompanyById(id);
    }


    /**
     * 用人单位注册
     *
     * @param vo
     * @return
     */
    public Long register(CompanyRegisterVo vo) {
        HireCompany hireCompany = new HireCompany();
        hireCompany.setAccount(vo.getPhoneNumber());
        hireCompany.setCode(vo.getCompanyCode());
        hireCompany.setPassword(SecureUtil.md5(vo.getPassword()));//加密登入密码
        hireCompany.setCreateTime(DateUtils.getNowDate());
        hireCompany.setCreateBy(vo.getPhoneNumber());
        hireCompany.setStatus(CompanyConstants.COMPANY_WAIT_FILL_STATUS);//待完善信息状态
        hireCompanyMapper.insertHireCompany(hireCompany);
        return hireCompany.getId();
    }


    /**
     * 查询用人单位管理
     *
     * @param phoneNumber
     * @return
     */
    public Long selectHireCompanyByAccount(String phoneNumber) {
        LambdaQueryWrapper<HireCompany> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(HireCompany::getAccount, phoneNumber);
        return hireCompanyMapper.selectCount(lambdaQueryWrapper);
    }
}
